package org.university.entity;

import io.swagger.annotations.ApiModelProperty;
import jakarta.persistence.*;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;

/**
 * @TableName teaching_class
 */
@Entity
@Table(name = "teaching_class")
@Data
public class TeachingClass implements Serializable {

    /**
     * 教室ID
     */
    @Id
    @NotNull(message = "[教室ID]不能为空")
    @ApiModelProperty("教室ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer classId;
    /**
     * 课程ID
     */
    @NotBlank(message = "[课程ID]不能为空")
    @Size(max = 20, message = "编码长度不能超过20")
    @ApiModelProperty("课程ID")
    @Length(max = 20, message = "编码长度不能超过20")
    private String courseId;
    /**
     * 教师ID
     */
    @NotBlank(message = "[教师ID]不能为空")
    @Size(max = 20, message = "编码长度不能超过20")
    @ApiModelProperty("教师ID")
    @Length(max = 20, message = "编码长度不能超过20")
    private String teacherId;
    /**
     * 学期
     */
    // todo 换一种比字符串更好的数据类型
    @NotBlank(message = "[学期]不能为空")
    @Size(max = 30, message = "编码长度不能超过30")
    @ApiModelProperty("学期")
    @Length(max = 30, message = "编码长度不能超过30")
    private String semester;
    /**
     * 最大学生量
     */
    @NotNull(message = "[最大学生量]不能为空")
    @ApiModelProperty("最大学生量")
    private Integer maxStudents;
    /**
     * 已有学生数量
     */
    @ApiModelProperty("已有学生数量")
    private Integer currentStudents;
    /**
     * 课程时间
     */
    @Size(max = 100, message = "编码长度不能超过100")
    @ApiModelProperty("课程时间")
    @Length(max = 100, message = "编码长度不能超过100")
    private String classTime;

    @Column(name = "deleted", columnDefinition = "tinyint(1) default 0")
    @ApiModelProperty("是否已删除")
    private Integer deleted = 0;

    // 学生退课事务，人数减一
    public void decreaseStudentCount() {
        if (this.currentStudents > 0) {
            this.currentStudents--;
        }
    }
}
